package com.sita.linboard.http;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.sita.linboard.DriverVehicle.SelectVehicleRequest;
import com.sita.linboard.DriverVehicle.UnBindVehicleRequest;
import com.sita.linboard.DriverVehicle.VehicleBackBean;
import com.sita.linboard.MainMessage.DeleteMsgBack;
import com.sita.linboard.MainMessage.DeleteMsgRequest;
import com.sita.linboard.MainMessage.DriverDayMsg;
import com.sita.linboard.MainMessage.DriverDayMsgRequest;
import com.sita.linboard.MainMessage.LastOrderBackBean;
import com.sita.linboard.calllist.GetOrderRequest;
import com.sita.linboard.calllist.VehicleListBackBean;
import com.sita.linboard.calllist.VehicleRequest;
import com.sita.linboard.driverInfo.ClearBackBean;
import com.sita.linboard.driverInfo.CommonRestBean;
import com.sita.linboard.driverInfo.MsgCenterBackBean;
import com.sita.linboard.driverInfo.MsgCenterRequest;
import com.sita.linboard.driverInfo.UpDataBackBean;
import com.sita.linboard.driverInfo.UpdateDriverLocationRequest;
import com.sita.linboard.driverInfo.VerificationCodeBean;
import com.sita.linboard.journey.OrderBackBean;
import com.sita.linboard.journey.OrderRequest;
import com.sita.linboard.journey.PostRouteCallBack;
import com.sita.linboard.login.ForgetPassBackBean;
import com.sita.linboard.login.ForgetPassRequest;
import com.sita.linboard.login.LoginBackBean;
import com.sita.linboard.login.LoginRequest;
import com.sita.linboard.login.RegisterBackBean;
import com.sita.linboard.login.RegisterRequest;
import com.sita.linboard.login.RegisterSecondBackBean;
import com.sita.linboard.person.DriverTripBackBean;
import com.sita.linboard.person.FeedBackRequest;
import com.sita.linboard.person.SettingBackBean;
import com.sita.linboard.utils.Constants;
import com.sita.linboard.wallet.BalanceRequest;
import com.sita.linboard.wallet.WalletBackBean;
import com.sita.linboard.wallet.WithdrawBackBean;
import com.squareup.okhttp.OkHttpClient;

import java.lang.reflect.Type;
import java.util.concurrent.TimeUnit;

import retrofit.Callback;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.Part;
import retrofit.http.Path;
import retrofit.http.Query;
import retrofit.mime.TypedFile;
import retrofit.mime.TypedString;

/**
 * Created by Jason on 2016/8/1.
 */
public class RestClient {
    private static final String BASE_URL = Constants.BASE_URI;
    private static RestService restService;
    private static RestService restNormalService;
    private static Gson mGson = new GsonBuilder()
            .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
            .serializeNulls()
            .registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
                @Override
                public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
                    if (src == src.longValue())
                        return new JsonPrimitive(src.longValue());
                    return new JsonPrimitive(src);
                }
            })
            .create();

    private static Gson mGson2 = new GsonBuilder()
            .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
            .create();

    private static RequestInterceptor restInterceptor = new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Content-Type", Constants.APPLICATION_JSON);
            request.addHeader("Accept", Constants.APPLICATION_JSON);
        }
    };

    private static RequestInterceptor restFormInterceptor = new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Accept", Constants.APPLICATION_JSON);
        }
    };

    private static void createRestFormClient() {
        final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setWriteTimeout(120, TimeUnit.SECONDS);

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(BASE_URL)
                .setConverter(new GsonConverter(mGson))
                .setRequestInterceptor(restFormInterceptor)
                .setClient(new OkClient(okHttpClient))
                .build();

        restService = restAdapter.create(RestService.class);
    }

    private static void createRestNormalClient() {
        final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
        okHttpClient.setWriteTimeout(120, TimeUnit.SECONDS);

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(BASE_URL)
                .setConverter(new GsonConverter(mGson))
                .setRequestInterceptor(restInterceptor)
                .setClient(new OkClient(okHttpClient))
                .build();

        restNormalService = restAdapter.create(RestService.class);
    }

    public static RestService getRestService() {
        if (restService == null) {
            createRestFormClient();
        }
        return restService;
    }

    public static RestService getRestNormalService() {
        if (restNormalService == null) {
            createRestNormalClient();
        }
        return restNormalService;
    }

    public static Gson getGson() {
        return mGson;
    }

    public static Gson getGsonNull() {
        return mGson2;
    }

    /**
     * 所有的请求直接返回实体类
     */
    public interface RestService {

        //  @GET("/usermgmt/v1/mileages/summaries?forSelf=true")
        //  void mileageSummaryForAccount(@QueryMap Map<String, String> params, Callback<MileageSummaryResponse> callback);

        //用户注册
        @POST("/linkboard/v1/actors/app/register")
        void register(@Body RegisterRequest registerRequest, Callback<RegisterBackBean> callback);

        //用户登录
        @POST("/linkboard/v1/actors/app/login")
        void login(@Body LoginRequest loginRequest, Callback<LoginBackBean> callback);

        //订单列表
        @POST("/linkboard/v1/trips/orderlist")
        void orderList(@Body VehicleRequest vehicleRequest, Callback<VehicleListBackBean> callback);

        //接单
        @POST("/linkboard/v1/trips/accept")
        void getOrder(@Body GetOrderRequest request, Callback<VehicleListBackBean> callback);

        //接到乘客
        @POST("/linkboard/v1/trips/departure")
        void getUser(@Body OrderRequest request, Callback<OrderBackBean> callback);

        //司机发起收款    参数相同不做修改
        @POST("/linkboard/v1/trips/receivables")
        void pay(@Body OrderRequest request, Callback<OrderBackBean> callback);

        //获取一个行程
        @POST("/linkboard/v1/trips/getone")
        void getOneOrder(@Body OrderRequest request, Callback<LastOrderBackBean> callback);

        //上传行程数据到达目的地
        @Multipart
        @POST("/linkboard/v1/trips/arrive")
        void postRoute(@Part("params") TypedString params, @Part("tripZip") TypedFile file, Callback<PostRouteCallBack> callback);

        //删除主页消息
        @POST("/linkboard/v1/inform/inform/delete")
        void deleteMsg(@Body DeleteMsgRequest deleteMsgRequest, Callback<DeleteMsgBack> callback);

        @POST("/linkboard/v1/actors/users/driver/fournum")
        void driverDayMsg(@Body DriverDayMsgRequest request, Callback<DriverDayMsg> callback);

        //个人设置
        @POST("/linkboard/v1/driver/driver/personal/setting")
        void MySetting(@Body DriverDayMsgRequest request, Callback<SettingBackBean> callback);

        //钱包
        @POST("/linkboard/v1/driver/driver/wallet/balance")
        void MyWallet(@Body BalanceRequest request, Callback<WalletBackBean> callback);

        //我的行程
        @POST("/linkboard/v1/trips/drivers/alltrips")
        void DriverTrip(@Body BalanceRequest request, Callback<DriverTripBackBean> callback);

        //消息中心
        @POST("/linkboard/v1/inform/inform/search")
        void MsgCenter(@Body MsgCenterRequest request, Callback<MsgCenterBackBean> callback);

        //清空消息中心
        @POST("/linkboard/v1/inform/inform/clean")
        void ClearAllMsg(@Body DriverDayMsgRequest request, Callback<ClearBackBean> callback);

        //绑定车辆列表
        @POST("/linkboard/v1/machines/machine/bind/list")
        void AllVehicle(@Body DriverDayMsgRequest request, Callback<VehicleBackBean> callback);

        //催单
        @POST("/linkboard/v1/trips/pressmoney")
        void urgedOrder(@Body OrderRequest request, Callback<ClearBackBean> callback);

        //忘记密码
        @POST("/linkboard/v1/actors/app/resetpassword")
        void forgetPassword(@Body ForgetPassRequest request, Callback<ForgetPassBackBean> callback);

        /*获取验证码——忘记密码*/
        @GET("/linkboard/v1/actors/message/authentication/{phonenum}")
        void getVerificationCode(@Path("phonenum") String phoneNum, @Query("type") int type, Callback<VerificationCodeBean> callback);

        //提现记录
        @POST("/linkboard/v1/driver/driver/withdrawals/history")
        void withdrawal(@Body BalanceRequest request, Callback<WithdrawBackBean> callback);

        //注册第二步
        @Multipart
        @POST("/linkboard/v1/driver/driver/register/two")
        void registerSecond(@Part("params") TypedString typedString, @Part("snnpicPositive") TypedFile positive, @Part("snnpicReverse") TypedFile reverse,
                            Callback<RegisterSecondBackBean> callback);//返回结果复用一个实体类

        @Multipart
        @POST("/linkboard/v1/machines/machine/bind/machine")
        void bindVehicle(@Part("params") TypedString typedString, @Part("pic") TypedFile typedFile, Callback<ClearBackBean> callback);

        ////返回结果复用一个实体类
        //修改用户信息
        @Multipart
        @POST("/linkboard/v1/actors/app/update")
        void upData(@Part("params") TypedString typedString, @Part("avatar") TypedFile typedFile, Callback<UpDataBackBean> callback);

        //实际反馈
        @POST("/linkboard/v1/feedback/feedback/add")
        void feedBack(@Body FeedBackRequest request, Callback<ClearBackBean> callback);

        //解绑车辆
        @POST("/linkboard/v1/machines/machine/unbind/machine")
        void UnBindVehicle(@Body UnBindVehicleRequest request, Callback<ClearBackBean> callback);

        //选中车辆
        @POST("/linkboard/v1/machines/machine/bind/select")
        void chooseVehicle(@Body SelectVehicleRequest request, Callback<ClearBackBean> callback);

        /*司机实时位置更新*/
        @POST("/linkboard/v1/driversRTP/driverRTP/modify")
        void updateDriverLocation(@Body UpdateDriverLocationRequest request, Callback<CommonRestBean> callback);

        /*获取软件版本*/
        @GET("/linkboard/v1/versions")
        void getVersion(@Query("platform") String platform, @Query("type") int type, Callback<CommonRestBean> callback);
    }

}
